﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GameVer1
{
    public class SoundOption
    {
        #region Attribute
        public Texture2D[] soundoptionTexture;
        public Texture2D[] buttonTexture;
        public int soundBackGround;
        public int soundBackGroundIcon;
        public int soundEffectOption;
        public int soundEffectOptionIcon;
        public int VolumeBackGroundState;
        public int VolumeEffectState;
        public float buttonConfirmScale;
        public float buttonCancelScale;
        public Vector2 buttonConfirmPos;
        public Vector2 buttonCancelPos;
        public Rectangle buttonConfirmRect;
        public Rectangle buttonCancelRect;
        public Rectangle mouseRect;
        public int lastSoundBackGround;
        public int lastSoundBackGroundIcon;
        public int lastSoundEffect;
        public int lastSoundEffectIcon;
        #endregion
        #region Constructor
        public SoundOption()
        {
        }
        public SoundOption(Texture2D[] texture,Texture2D[] textureButton)
        {
            buttonConfirmPos.X = 250;
            buttonConfirmPos.Y = 500;
            buttonCancelPos.X = 400;
            buttonCancelPos.Y = 500;
            soundoptionTexture = texture;
            buttonTexture = textureButton;
            soundBackGround = soundEffectOption = 4;
            soundBackGroundIcon = soundEffectOptionIcon = 0;
            buttonConfirmScale = 1;
            buttonCancelScale = 1;
            buttonCancelRect = new Rectangle((int)buttonCancelPos.X, (int)buttonCancelPos.Y, buttonTexture[0].Width, buttonTexture[0].Height);
            buttonConfirmRect = new Rectangle((int)buttonConfirmPos.X,(int)buttonConfirmPos.Y,buttonTexture[1].Width,buttonTexture[1].Height);
        }
        #endregion
        #region Menthod
        public void Draw(SpriteBatch spriteBatch,SpriteFont spriteFont)
        {
            spriteBatch.DrawString(spriteFont,"Volume Background" ,new Vector2(30, 10),
                 Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(soundoptionTexture[soundBackGroundIcon], new Vector2(100, 50),
                new Rectangle(0, 0, soundoptionTexture[soundBackGroundIcon].Width, soundoptionTexture[soundBackGroundIcon].Height), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(soundoptionTexture[soundBackGround], new Vector2(150, 50),
                new Rectangle(0, 0, soundoptionTexture[soundBackGround].Width, soundoptionTexture[soundBackGround].Height), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

            spriteBatch.DrawString(spriteFont, "Volume SoundEffect", new Vector2(30, 130),
                 Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(soundoptionTexture[soundEffectOptionIcon], new Vector2(100, 150),
                new Rectangle(0, 0, soundoptionTexture[soundEffectOptionIcon].Width, soundoptionTexture[soundEffectOptionIcon].Height), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.Draw(soundoptionTexture[soundEffectOption], new Vector2(150, 150),
                new Rectangle(0, 0, soundoptionTexture[soundEffectOption].Width, soundoptionTexture[soundEffectOption].Height), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

            spriteBatch.Draw(buttonTexture[1], buttonConfirmPos,
                new Rectangle(0, 0, buttonTexture[1].Width, buttonTexture[1].Height), Color.White, 0, Vector2.Zero, buttonConfirmScale, SpriteEffects.None, 0);
            spriteBatch.Draw(buttonTexture[0], buttonCancelPos,
                new Rectangle(0, 0, buttonTexture[0].Width, buttonTexture[0].Height), Color.White, 0, Vector2.Zero, buttonCancelScale, SpriteEffects.None, 0);
        }
        public void Update(GameTime gameTime,MouseState mouse,ref int returnvalue)
        {
        
            if (mouse.LeftButton != 0)
            {
                mouseRect = new Rectangle(mouse.X, mouse.Y, 1, 1);
                if (180 < mouse.X && mouse.X < 200 && 60 < mouse.Y && mouse.Y < 80)
                {
                    soundBackGroundIcon = 2;
                    soundBackGround = 6;
                    VolumeBackGroundState = 0;
                }
                else if (250 < mouse.X && mouse.X < 270 && 60 < mouse.Y && mouse.Y < 80)
                {
                    soundBackGroundIcon = 3;
                    soundBackGround = 7;
                    VolumeBackGroundState = 1;
                }
                else if (330 < mouse.X && mouse.X < 350 && 60 < mouse.Y && mouse.Y < 80)
                {
                    soundBackGroundIcon = 1;
                    soundBackGround = 5;
                    VolumeBackGroundState = 2;
                }
                else if (410 < mouse.X && mouse.X < 430 && 60 < mouse.Y && mouse.Y < 80)
                {
                    soundBackGroundIcon = 0;
                    soundBackGround = 4;
                    VolumeBackGroundState = 3;
                }
                else if (180 < mouse.X && mouse.X < 200 && 160 < mouse.Y && mouse.Y < 180)
                {
                    soundEffectOptionIcon = 2;
                    soundEffectOption = 6;
                }
                else if (250 < mouse.X && mouse.X < 270 && 160 < mouse.Y && mouse.Y < 180)
                {
                    soundEffectOptionIcon = 3;
                    soundEffectOption = 7;
                }
                else if (330 < mouse.X && mouse.X < 350 && 160 < mouse.Y && mouse.Y < 180)
                {
                    soundEffectOptionIcon = 1;
                    soundEffectOption = 5;
                }
                else if (410 < mouse.X && mouse.X < 430 && 160 < mouse.Y && mouse.Y < 180)
                {
                    soundEffectOptionIcon = 0;
                    soundEffectOption = 4;
                }
                else if (mouseRect.Intersects(buttonConfirmRect))
                {
                    buttonConfirmScale = (float)0.95;
                    returnvalue = 1;
                    UpdateLastSoundOption();
                }
                else if (mouseRect.Intersects(buttonCancelRect))
                {
                    buttonCancelScale = (float)0.95;
                    returnvalue = 0;
                    GetLastSoundOption();
                }
            }
            else
            {
                buttonConfirmScale = 1;
                buttonCancelScale = 1;
            }
        }
        public void GetLastSoundOption()
        {
            soundEffectOption = lastSoundEffect;
            soundEffectOptionIcon = lastSoundEffectIcon;
            soundBackGround = lastSoundBackGround;
            soundBackGroundIcon = lastSoundBackGroundIcon;
        }
        public void UpdateLastSoundOption()
        {
            lastSoundBackGround = soundBackGround;
            lastSoundBackGroundIcon = soundBackGroundIcon;
            lastSoundEffect = soundEffectOption;
            lastSoundEffectIcon = soundEffectOptionIcon;
        }
        #endregion
    }
}
